#include "NanoBrowserWindow.h"
#include "NanoCefClient.h"

using namespace std::literals;

static constexpr const LPCTSTR wndClassName = TEXT("$browser-window$");
static CefRefPtr<NanoCefClient> pClient;

COLORREF transparentColor = RGB(255, 255, 255);
LRESULT CALLBACK BrowserWindowWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg)
    {
    case WM_CREATE:
    {
        SetWindowLongPtr(hWnd, GWL_EXSTYLE, GetWindowLongPtr(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
        SetLayeredWindowAttributes(hWnd, transparentColor, 225, LWA_COLORKEY | LWA_ALPHA);

        pClient = new NanoCefClient();
        RECT rect{};
        GetClientRect(hWnd, &rect);
        CefRect cefRect;
        cefRect.x = rect.left;
        cefRect.y = rect.top;
        cefRect.width = rect.right - rect.left;
        cefRect.height = rect.bottom - rect.top;

        CefWindowInfo info;
        info.SetAsChild(hWnd, cefRect);

        //CefBrowserHost::CreateBrowser(info, pClient, "https://www.youtube.com"s, {}, {}, {});
        //CefBrowserHost::CreateBrowser(info, pClient, std::string("https://www.bing.com"), {}, {}, {});
        //CefBrowserHost::CreateBrowser(info, pClient, std::string("https://www.w3schools.com/html/mov_bbb.mp4"), {}, {}, {});
        //CefBrowserHost::CreateBrowser(info, pClient, std::string("https://www.w3schools.com/html/mov_bbb.webm"), {}, {}, {});
        CefBrowserHost::CreateBrowser(info, pClient, "http://disk/index.html"s, {}, {}, {});
        break;
    }
    case WM_SIZE:
    {
        if (wParam != SIZE_MINIMIZED && pClient)
        {
            if (auto pBrowser = pClient->GetBrowser())
            {
                if (auto hWndBrowser = pBrowser->GetHost()->GetWindowHandle())
                {
                    RECT rect{};
                    GetClientRect(hWnd, &rect);
                    SetWindowPos(hWndBrowser, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER);
                }
            }
        }
        break;
    }
    case WM_ERASEBKGND:
    {
        if (pClient)
        {
            if (auto pBrowser = pClient->GetBrowser())
            {
                if (pBrowser->GetHost()->GetWindowHandle())
                {
                    return 1;
                }
            }
        }
        break;
    }
    case WM_LBUTTONDOWN:
    {
        PostMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
        break;
    }
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    default:
        break;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

HWND CreateBrowserWindow(HINSTANCE hInstance)
{
    HWND hWndBrowser = nullptr;
    WNDCLASSEX wcex{};
    wcex.cbSize = sizeof(wcex);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = BrowserWindowWndProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = CreateSolidBrush(transparentColor);
    wcex.lpszClassName = wndClassName;
    RegisterClassEx(&wcex);

    hWndBrowser = CreateWindowEx(
        0, wndClassName, TEXT("Nano CEF"),
        WS_POPUP | WS_CLIPCHILDREN, 200, 20,
        1360, 1020, nullptr, nullptr, hInstance, nullptr
    );

    ShowWindow(hWndBrowser, SW_SHOWDEFAULT);
    UpdateWindow(hWndBrowser);

    return hWndBrowser;
}

void CleanupBrowserWindow(HINSTANCE hInstance)
{
    pClient.reset();
    UnregisterClass(wndClassName, hInstance);
}

